package utils

import (
	"fmt"
	"net/http"
	"os"
	"strings"
)

type TgInfo struct {
	Token   string `json:"token"`
	ChatId  string `json:"chatId"`
	Message string `json:"message"`
}

func NewTgInfo(token, chatId, message string) *TgInfo {
	return &TgInfo{
		Token:   token,
		ChatId:  chatId,
		Message: message,
	}
}

func (s *TgInfo) SendMes() bool {
	api := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", s.Token)
	body := strings.NewReader(fmt.Sprintf("chat_id=%s&text=%s", s.ChatId, s.Message))
	req, err := http.NewRequest("POST", os.ExpandEnv(api), body)
	if err != nil {
		return false
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return false
	}
	defer resp.Body.Close()
	return true
}
